package com.ssm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ssm.mapper.MaterialMapper;
import com.ssm.mapper.TeacherMapper;
import com.ssm.model.GradeModel;
import com.ssm.model.LoginInfo;
import com.ssm.model.MaterialTemplateModel;
import com.ssm.model.TeacherMaterialModel;
import com.ssm.model.TeacherModel;
import com.ssm.service.MaterialService;
import com.ssm.service.TeacherService;

@Service
public class TeacherServiceImpl implements TeacherService {

	@Autowired
	private TeacherMapper teacherMapper;
	@Autowired
	private MaterialService materialService;
	@Autowired
	private MaterialMapper materialMapper;

	@Override
	public Page<TeacherModel> selectTeacher(Integer pageNum,String teacherName,Integer tGrade) {
		// TODO Auto-generated method stub
		PageHelper.startPage(pageNum, 5);
		HashMap<String, Object> map = new HashMap<>();
		map.put("tName", teacherName);
		map.put("tGrade", tGrade);
		Page<TeacherModel> teacherModels = (Page<TeacherModel>)teacherMapper.selectTeacher(map);
		/*for (TeacherModel teacherModel : teacherModels) {
			Integer gId = teacherModel.getGradeModel().getgId();
			GradeModel gradeModel = teacherMapper.selectGradeById(gId);
			teacherModel.setGradeModel(gradeModel);
		}*/
		return teacherModels;
	}
	

	@Override
	public LoginInfo selectById(Integer id) {
		// TODO Auto-generated method stub
		return teacherMapper.selectById(id);
	}

	

	@Override
	public Integer insertTeacher(LoginInfo loginInfo) {
		// TODO Auto-generated method stub
		return teacherMapper.insertTeacher(loginInfo);
	}

	
	@Override
	public Integer updateTeacher(HashMap<String, Object> map) {
		// TODO Auto-generated method stub
		return teacherMapper.updateTeacher(map);
	}

	@Override
	public Integer deleteTeacher(Integer teacherId) {
		// TODO Auto-generated method stub
		return teacherMapper.deleteTeacher(teacherId);
	}

	@Override
	public Integer insertAssessPlan(Integer tId, Integer pId) {
		// TODO Auto-generated method stub
		HashMap<String, Object> map = new HashMap<>();
		map.put("tId", tId);
		map.put("pId", pId);
		Integer i = teacherMapper.insertAssessPlan(map);
		if(i!=0){//分配assessPlan后，查询tmid更新到assessPlan中
			TeacherMaterialModel teacherMaterial = materialMapper.selectTeacherMaterial(tId);
			HashMap map2 = new HashMap<>();
			map2.put("tId", tId);
			map2.put("tmId", teacherMaterial.getTmId());
			teacherMapper.updateTmIdOnPlan(map2);
		}
		return null;
	}


	@Override
	public HashMap selectAssessByTid(Integer tId) {
		// TODO Auto-generated method stub
		HashMap resultMap = teacherMapper.selectAssessByTid(tId);
		/*TeacherModel teacherModel = (TeacherModel)resultMap.get("teacherModel");
		Integer gId = teacherModel.getGradeModel().getgId();
		GradeModel gradeModel = teacherMapper.selectGradeById(gId);
		teacherModel.setGradeModel(gradeModel);
		resultMap.replace("teacherModel", teacherModel);*/
		return resultMap;
	}

	@Override
	public List<GradeModel> selectGrades() {
		// TODO Auto-generated method stub
		return teacherMapper.selectGrades();
	}

	@Override
	public TeacherModel selectCourseByTid(Integer tId) {
		// TODO Auto-generated method stub
		return teacherMapper.selectCourseByTid(tId);
	}

	@Override
	public Integer checkCourseByTid(Integer tId) {
		// TODO Auto-generated method stub
		return teacherMapper.checkCourseByTid(tId);
	}

	@Override
	public Integer insertCourse(String courseData, Integer tId) {
		// TODO Auto-generated method stub
		HashMap map = new HashMap<>();
		map.put("courseData", courseData);
		map.put("tId", tId);
		return teacherMapper.insertCourse(map);
	}

	@Override
	public Integer updateCourse(String courseData, Integer tId) {
		// TODO Auto-generated method stub
		HashMap map = new HashMap<>();
		map.put("courseData", courseData);
		map.put("tId", tId);
		return teacherMapper.updateCourse(map);
	}


	/* front */
	@Override
	public TeacherModel selectTeacherById(Integer tId) {
		// TODO Auto-generated method stub
		TeacherModel teacherModel = teacherMapper.selectTeacherById(tId);
		/*Integer gid = teacherModel.getGradeModel().getgId();
		GradeModel gradeModel = teacherMapper.selectGradeById(gid);
		teacherModel.setGradeModel(gradeModel);*/
		return teacherModel;
	}

	//完善个人信息
	@Override
	public Integer updateInfo(TeacherModel teacherModel) {
		// TODO Auto-generated method stub
		Integer i = teacherMapper.updateInfo(teacherModel);
		if(i!=0){
			//个人信息完善成功，自动完善其他模板参数：材料参数
			//调用MaterialService.selectMaterialTemplate()获得材料参数集合
			List templateList = materialService.selectMaterialTemplate();
			Map materialMap = new HashMap<>();
			materialMap.put("root", templateList);
			String jsonString = JSON.toJSONString(materialMap);
			HashMap paramMap = new HashMap<>();
			paramMap.put("tmContent", jsonString);
			paramMap.put("tId", teacherModel.gettId());
			
			materialMapper.insertTeacherMaterial(paramMap);
		}
		return i;
	}


	@Override
	public Integer updateVerifyByEmail(Integer tId,String email, String verify) {
		// TODO Auto-generated method stub
		HashMap map = new HashMap<>();
		map.put("tId", tId);
		map.put("tEmail", email);
		map.put("verify", verify);
		return teacherMapper.updateVerifyByEmail(map);
	}


	@Override
	public Integer checkVerify(Integer tId,String tEmail, String tVerify) {
		// TODO Auto-generated method stub
		HashMap map = new HashMap<>();
		map.put("tId", tId);
		map.put("tEmail", tEmail);
		map.put("tVerify", tVerify);
		return teacherMapper.checkVerify(map);
	}
	
	
	
}
